package com.ruoyi.socket.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gams.domain.GamsStaff;
import com.ruoyi.gams.domain.GamsUserQueue;
import com.ruoyi.gams.domain.GamsWindow;
import com.ruoyi.gams.service.IGamsStaffService;
import com.ruoyi.gams.service.IGamsUserQueueService;
import com.ruoyi.gams.service.IGamsWindowService;
import com.ruoyi.gams.vo.Result;
import com.ruoyi.gams.vo.param.FunctionParam;
import com.ruoyi.gams.vo.param.StaffLoginOutParam;
import com.ruoyi.gams.vo.param.StaffLoginParam;
import com.ruoyi.socket.handler.CallDeviceMessageHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "2.呼叫器接口")
@RestController
@RequestMapping("/call-device")
public class CallDeviceController extends BaseController
{

    @Autowired
    private IGamsStaffService staffService;

    @Autowired
    private IGamsWindowService windowService;

    @Autowired
    private CallDeviceMessageHandler callDeviceMessageHandler;

    @Autowired
    private IGamsUserQueueService userQueueService;

    @ApiOperation("2.0 登录")
    @ResponseBody
    @PostMapping("/login")
    public Result<GamsStaff> login(@RequestBody StaffLoginParam param)
    {
        log.info("param = " + JSONObject.toJSONString(param));
        param.setStaffNo(param.getStaffNo().trim());
        param.setWindowName(param.getWindowName().trim());
        if (StringUtils.isEmpty(param.getStaffNo()))
        {
            throw new GlobalException("员工编号不能为空");
        }
        GamsStaff staff = staffService.findFirstByStaffNo(param.getStaffNo().toUpperCase().trim());
        if (staff == null)
        {
            throw new GlobalException("员工编号不正确");
        }
        if (param.getPassword() == null || !param.getPassword().equals(staff.getPassword()))
        {
            throw new GlobalException("员工密码不正确");
        }

        GamsWindow window = windowService.findFirstByName(param.getWindowName());

        if (window == null)
        {
            throw new GlobalException("窗口输入错误");
        }
        //先退出登录
        GamsWindow firstByStaffId = windowService.findFirstByStaffId(staff.getId());
        if (firstByStaffId != null)
        {
            windowService.updateStaffIdAndStaffNameById(null, null, firstByStaffId.getId());
        }

        //更新窗口信息
        window.setStaffId(staff.getId());
        window.setStaffName(staff.getStaffName());
        window.setUpdateTime(new Date());
        windowService.updateGamsWindow(window);
        return Result.success(staff);
    }

    @ApiOperation("2.1 退出登录")
    @ResponseBody
    @PostMapping("/loginout")
    public Result<GamsStaff> loginout(@RequestBody StaffLoginOutParam param)
    {
        GamsStaff staff = staffService.findFirstByStaffNo(param.getStaffNo());
        if (staff == null)
        {
            throw new GlobalException("员工编号不正确");
        }
        GamsWindow window = windowService.findFirstByStaffId(staff.getId());
        if (window != null)
        {
            windowService.updateStaffIdAndStaffNameById(null, null, window.getId());
        }
        return Result.success();
    }

    @ApiOperation("2.2 功能")
    @ResponseBody
    @PostMapping("/function")
    public Result<GamsStaff> function(@RequestBody FunctionParam param)
    {
        log.info("Call param = " + JSONObject.toJSONString(param));
        String windowName = param.getWindowName();
        GamsWindow window = windowService.findFirstByName(windowName);

        if (window == null)
        {
            throw new GlobalException("窗口不存在:" + windowName);
        }
        callDeviceMessageHandler.call(param.getCmd(), window);
        return Result.success();
    }

    @ApiOperation("2.3 等候列表")
    @ResponseBody
    @GetMapping("/waitlist")
    public TableDataInfo waitlist(GamsUserQueue param)
    {
        GamsWindow gamsWindow = windowService.findFirstByStaffId(param.getStaffId());
        if (gamsWindow == null)
        {
            throw new GlobalException("窗口不存在,员工未在任何窗口登录");
        }

        startPage();
        //今天的数据
        param.setIsToday(1);
        String businessQueueIds = gamsWindow.getBusinessQueueIds();

        //排除没有绑定业务的窗口
        if (businessQueueIds == null)
        {
            return getDataTable(new ArrayList<>());
        }
        String[] ids = businessQueueIds.split(",");
        param.setBussinessIds(ids);
        param.setWaiting(1);
        param.setStaffId(null);
        List<GamsUserQueue> list = userQueueService.selectGamsUserQueueList(param);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @ApiOperation("2.4 已叫号列表")
    @ResponseBody
    @GetMapping("/calledlist")
    public TableDataInfo calledlist(GamsUserQueue param)
    {
        GamsWindow gamsWindow = windowService.findFirstByStaffId(param.getStaffId());
        if (gamsWindow == null)
        {
            throw new GlobalException("窗口不存在,员工未在任何窗口登录");
        }
        startPage();
        GamsUserQueue queue = new GamsUserQueue();
        queue.setWindowId(gamsWindow.getId());
        queue.setIsToday(1);//今天的数据
        List<GamsUserQueue> list = userQueueService.selectGamsUserQueueListDesc(queue);
        return getDataTable(list);
    }


}
